package store.lunangangster.nodeflow.parser.el.operator;

import cn.hutool.core.collection.CollUtil;
import com.ql.util.express.Operator;
import java.util.ArrayList;
import java.util.List;
import store.lunangangster.nodeflow.exception.RuleSourceParseException;
import store.lunangangster.nodeflow.parser.el.OperatorHelper;
import store.lunangangster.nodeflow.rt.element.Executable;
import store.lunangangster.nodeflow.rt.element.flow.SwitchFlow;
import store.lunangangster.nodeflow.util.Converter;

/**
 * 用于扩展SwitchFlow的TO操作符
 *
 * @author cwk
 * @version 1.0
 */
public class ToOperator extends Operator {

  @Override
  public Object executeInner(Object[] list) throws RuleSourceParseException {
    OperatorHelper.checkArguments(list);

    SwitchFlow switchFlow = Converter.convert(list[0], SwitchFlow.class);
    if (!CollUtil.isEmpty(switchFlow.getExecutableList())) {
      throw new RuleSourceParseException("Incompatible executable for SwitchFlow: SwitchFlow already has ToBody, cannot use TO() again");
    }
    List<Executable> executableList = new ArrayList<>();
    for (int i = 1; i < list.length; i++) {
      executableList.add(Converter.convert(list[i], Executable.class));
    }
    switchFlow.setExecutableList(executableList);
    return switchFlow;
  }
}
